package com.supersit.GZFDA.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.supersit.GZFDA.R;
import com.supersit.GZFDA.adapter.UnUpLoadDataAdatper.Callback;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class ShowWindows {
	List<Map<String, String>> moreList;
	private PopupWindow pwMyPopWindow;// popupwindow
	private ListView lvPopupList;// popupwindow中的ListView
	private int NUM_OF_VISIBLE_LIST_ROWS = 2;// 指定popupwindow中Item的数量
	private Context context;
	private int type = 1;// 1.数据上传 2.街道搜索
	private Callback mCallback;

	public ShowWindows(Context context, Callback mCallback) {
		super();
		this.context = context;
		this.mCallback = mCallback;
		initData();
		iniPopupWindow();
	}

	public void showPopupWindow(View view) {
		if (pwMyPopWindow.isShowing()) {
			pwMyPopWindow.dismiss();
		} else {
			pwMyPopWindow.showAsDropDown(view);
		}
	}

	private void iniPopupWindow() {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.task_detail_popupwindow, null);
		lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
		pwMyPopWindow = new PopupWindow(layout);
		pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件

		lvPopupList.setAdapter(new SimpleAdapter(context, moreList,
				R.layout.list_item_popupwindow, new String[] { "share_key" },
				new int[] { R.id.tv_list_item }));
		lvPopupList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mCallback.onItemClick(parent, view, position, id);
				if ("数据上传".equals(moreList.get(position).get("share_key"))) {
					type = 1;
				} else if ("新增企业".equals(moreList.get(position)
						.get("share_key"))) {
					type = 2;
				}
				pwMyPopWindow.dismiss();
			}
		});

		// 控制popupwindow的宽度和高度自适应
		lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,
				View.MeasureSpec.UNSPECIFIED);
		pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
		pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight() + 20)
				* NUM_OF_VISIBLE_LIST_ROWS);

		// 控制popupwindow点击屏幕其他地方消失
		pwMyPopWindow.setBackgroundDrawable(context.getResources().getDrawable(
				R.drawable.bg_popupwindow));// 设置背景图片，不能在布局中设置，要通过代码来设置
		pwMyPopWindow.setOutsideTouchable(true);// 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上

	}

	private void initData() {
		moreList = new ArrayList<Map<String, String>>();
		Map<String, String> map;
		map = new HashMap<String, String>();
		map.put("share_key", "数据上传");
		moreList.add(map);
		map = new HashMap<String, String>();
		map.put("share_key", "新增企业");
		moreList.add(map);
	}

	public int showNum() {
		return type;
	}

	/**
	 * 自定义接口，用于回调按钮点击事件到Activity
	 * 
	 * @author Ivan Xu 2014-11-26
	 */
	public interface Callback {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id);
	}
}
